package com.util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.dto.CreditDto;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CreditUtile {
	//CreditUtile.Get(realname,idcard);调用征信时候action里加进去

	public static final String APPKEY = "a1d44d672675ef5a";// ���appkey
    public static final String URL = "http://api.jisuapi.com/creditblacklist/query";
//    public static final String realname = "";
//    public static final String idcard = "";
  	  //  private String realname;
	  //  private String idcard;
//李尚钟   532722197511040914

	public static List<CreditDto> Get(String realname,String idcard) throws Exception {
        String result = null;
        String url = URL + "?realname=" + URLEncoder.encode(realname,"utf-8") + "&idcard=" + idcard + "&appkey=" + APPKEY;
        List<CreditDto> CreditList = new ArrayList<CreditDto>();
        try {
            result = HttpUtils.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = json.optJSONObject("result");
                realname = resultarr.getString("realname");
                idcard = resultarr.getString("idcard");
                System.out.println(realname + " " + idcard);
                if (resultarr.opt("list") != null) {
                    JSONArray list = resultarr.optJSONArray("list");
                    for (int i = 0; i < list.size(); i++) {
                        JSONObject obj = (JSONObject) list.opt(i);
                        CreditDto dto=(CreditDto)JSONObject.toBean(obj, CreditDto.class);
                        CreditList.add(dto);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
		return CreditList;
	}
	public static void main(String[] args) throws Exception {
		String realname = "李尚钟";
		String idcard = "532722197511040914";
		List<CreditDto> CreditList = CreditUtile.Get(realname,idcard);
		for (CreditDto creditDto : CreditList) {
			System.err.println(creditDto);
		}
	}

}
